package game.farming.ui;

import game.farming.model.OptionToBuy;
import game.farming.model.Product;
import game.farming.model.Table;

import java.util.HashMap;
import java.util.Stack;
import java.util.Vector;

import org.apache.myfaces.trinidad.component.core.output.CoreImage;

import test.P;

public class Player {
	private Table table;
	private String name;
	private int cash;
	private int loan;
	private Vector<Product> products;
	private HashMap<Product, Double> yields;
	private Stack<OptionToBuy> otbs;
	private int pos;
	private boolean turn = true;
	private CoreImage playerImage;
	
	public Player() {
		name = "PlayerName";
		table = new Table("TableName");
		table.addPlayer(this);
		init();
	}
	
	public void init() {
		playerImage = new CoreImage();
		playerImage.setSource("images/pieces/p1.gif");
		gameInit();
	}
	
	public void gameInit() {
		cash = 5000;
		loan = 10000;
		products = new Vector<Product>();
		for (int i = 0; i < 10; i++) {
			products.add(Product.HAY);
			products.add(Product.GRAIN);
		}
		yields = new HashMap<Product, Double>();
		otbs = new Stack<OptionToBuy>();
		pos = 0;
		yearInit();
	}
	
	public void yearInit() {
		cash += 5000;
		for (Product p : Product.PRODUCTS) {
			yields.put(p, 1.0);
		}
	}
	
	public String roll() {
		int roll = table.getDice().next();
		moveAhead(roll);
		return "";
	}
	
	private void moveAhead(int roll) {
		table.getBoard().get(pos).getChildren().remove(playerImage);
		
		pos += roll;
		if (pos >= table.getBoard().size()) {
			pos = pos % table.getBoard().size();
			yearInit();
		}
		table.getBoard().get(pos).performAction(this, table);
		
		String source = playerImage.getSource();
		playerImage = new CoreImage();
		playerImage.setSource(source);
		table.getBoard().get(pos).getChildren().add(playerImage);
		DEBUG();
	}
	
	public void DEBUG() {
		System.out.print("Pos: " + pos);
		System.out.print(", Cash: " + cash);
		System.out.print(", Loan: " + loan);
		System.out.println();
	}
	
	public String joinTable() {
		//call app bean join(tableName, this);
		return "";
	}
	
	public String leaveTable() {
		return "";
	}
	
	public void addCash(int amount) {
		cash += amount;
	}
	
	public int getProductCount(Product p) {
		int count = 0;
		for (Product prod : products) {
			if (prod.equals(p)) {
				count++;
			}
		}
		return count;
	}
	
	public boolean hasProduct(Product p) {
		return products.contains(p);
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public int getCash() {
		return cash;
	}
	
	public void setCash(int cash) {
		this.cash = cash;
	}
	
	public int getLoan() {
		return loan;
	}
	
	public void setLoan(int loan) {
		this.loan = loan;
	}
	
	public Vector<Product> getProducts() {
		return products;
	}
	
	public void setProducts(Vector<Product> products) {
		this.products = products;
	}
	
	public HashMap<Product, Double> getYields() {
		return yields;
	}
	
	public void setYields(HashMap<Product, Double> yields) {
		this.yields = yields;
	}
	
	public void addOtb(OptionToBuy otb) {
		otbs.push(otb);
	}
	
	public void moveTo(int sq) {
		pos = sq;
	}
	
	public void harvestProduct(Product p, double yield) {
		double amount = yield;
		amount *= yields.get(p);
		amount *= p.getPricePerUnit();
		amount *= getProductCount(p);
		amount *= table.getDice().next();
		P.p("Harvest: " + p.getName() + ", " + amount + " = " + yield + " * " + yields.get(p) + " * " + p.getPricePerUnit() + " * " + getProductCount(p) + " * " + table.getDice().last());
		cash += amount;
	}
	
	public Table getTable() {
		return table;
	}
	
	public void setTable(Table table) {
		this.table = table;
	}
	
	public Stack<OptionToBuy> getOtbs() {
		return otbs;
	}
	
	public void setOtbs(Stack<OptionToBuy> otbs) {
		this.otbs = otbs;
	}
	
	public int getPos() {
		return pos;
	}
	
	public void setPos(int pos) {
		this.pos = pos;
	}
	
	public CoreImage getPlayerImage() {
		return playerImage;
	}
	
	public void setPlayerImage(CoreImage playerImage) {
		this.playerImage = playerImage;
	}

	public boolean isTurn() {
		return turn;
	}

	public void setTurn(boolean turn) {
		this.turn = turn;
	}
}
